module App::Controller
    class MainWindow < Qt::Object
        slots :openToggl, :configurePlugins, :quit
        
        attr_reader :model, :view
        attr_reader :dropwindow # atmenetileg kell
        attr_reader :treecontroller

        TogglUrl = "https://www.toggl.com"
        
        def initialize(model)
            super()

            $model = @model = model

            # Many users. eg the App::Controller::New
            $worker = model.tree

            @adaptor = App::Controller::DbusAdaptor.new $qApp

            @parsers = []
            $parsers.each { |p|
                @parsers << p.new(model.tree)
            }

            # A modelColumnsModified() -nek kell az oszlopok szelessegenek
            # ujrabeallitasahoz.
            @tv = tv = App::View::TogglTree.new
            tv.setModel model.tree
            @treecontroller = App::Controller::TogglTree.new tv

            dropwindow = App::Controller::DropWindow.new(self, model.load_setting("dropwindow_geometry"))
            # Quit() -ban kell, menteshez.
            @dropwindow = dropwindow

            @watchdog_observer = App::Controller::WatchdogObserver.new(model.watchdog)
            @watchdog_observer_view = App::View::WatchdogObserver.new(model.watchdog)

            @tray = App::Controller::TrayIcon.new(model)

            @view = view = App::View::MainWindow.new(tv, model.load_setting("geometry"))
            view.reload_button.connect(SIGNAL(:triggered)) { model.tree.reload }
            view.toggl_web_button.connect(SIGNAL(:triggered)) { openToggl }
            view.configure_parsers_button.connect(SIGNAL(:triggered)) { configurePlugins }
            view.exit_button.connect(SIGNAL(:triggered)) { quit }
            sc = Qt::Shortcut.new Qt::KeySequence.new(Qt::Key_Escape), view
            sc.connect(SIGNAL(:activated)) { model.tree.stopAllTasks }
            
            connect(model.watchdog, SIGNAL('taskRunning(QObject*, int)'),
                view, SLOT('taskRunning(QObject*, int)'))
            connect(model.watchdog, SIGNAL('taskStopped()'),
                view, SLOT('taskStopped()'))
        end

        def trayIconActivated()
            if @view.isVisible and @view.isActiveWindow
                @geometry = @view.geometry
                @view.hide
            else
                if not @view.isVisible
                    @view.hide
                    @view.show
                else
                    @view.hide
                    @view.show
                end
            end
        end

        private
        def configurePlugins()
            @settings = App::Controller::Settings.new
            return

            # Unused feature.
            menu = Qt::Menu.new "configurePlugins"
            menu.addAction("Settings...").connect(SIGNAL(:triggered)) { App::Controller::Settings.new }
            @parsers.each { |p|
                menu.addAction(p.class.to_s).connect(SIGNAL(:triggered), p, SLOT(:config))
            }
            #point.setX(point.x + 5)
            #point.setY(point.y + @tv.header.height)
            menu.popup @view.toolbar.mapToGlobal(Qt::Point.new(0,0))
        end

        def openToggl()
            Qt::DesktopServices::openUrl Qt::Url.new(TogglUrl)
        end

        def quit()
            @model.save_setting "geometry", @view.geometry
            @model.save_setting "dropwindow_geometry", @dropwindow.view.geometry
            Qt::CoreApplication.instance.quit
        end
    end
end
